<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
use \Common\Model\Cate;
use \Common\Model\Goods;
use \Common\Model\GoodsAttr;

class CateController extends Controller{
	//构造函数
	public function __init()
	{
	}
	
	
	
	//动作
    public function index(){
    	
       //1.获取顶级分类
        $cateModel = new Cate;
        $topCate = $cateModel->where("pid=0")->get();
        View::with('topCate',$topCate);

        //2.通过当前分类的cid获得所有子集的cid
        $cid = Q('get.cid',0,'intval');
        $cids = $cateModel->getSon($cateModel->get(),$cid);
        $cids[] = $cid;

        //3.根据cid获得商品的gid
        $goodsModel = new Goods();
        $gids = $goodsModel->where("cid IN(" . implode(',',$cids) . ")")->lists('gid');
        if($gids){
            //4.通过gid到goods_attr表查询属性
            $goodsAttrModel = new GoodsAttr();
            $goodsAttr = $goodsAttrModel->where("gid IN(" . implode(',',$gids) . ")")->groupBy('gtvalue')->get();
//			p($goodsAttr);
            //把相同taid的属性放在了一起
            $sameTemp = [];
            foreach ($goodsAttr as $v){
                $sameTemp[$v['taid']][] = $v;
            }
            //组合名字和值
//          p($sameTemp);
            $finalAttr = [];
            foreach ($sameTemp as $taid => $v){
                $finalAttr[] = [
                    'name' => $this->gettname($taid),
                    'value'=> $v
                ];
            }
            View::with('finalAttr',$finalAttr);
			//筛选效果********************
            //1.地址栏效果0_0_...
            $num = count($finalAttr);
            $param = isset($_GET['param']) ? explode('_',$_GET['param']) : array_fill(0,$num,0);
            View::with("param",$param);
            //2.筛选操作,最终获得商品gid
            $finalGids = $this->filter($param,$gids);
//          p($finalGids);
			$goods=$goodsModel->where("gid IN (".implode(',', $finalGids).")")->get();
//			p($goods);
			 View::with('goods',$goods);
        }else{
            View::with('finalAttr',[]);
			View::with('goods',[]);
        }
		//p($finalAttr);
        View::make();
    }

	private function gettname($taid){
		return Db::table('type_attr')->where("taid={$taid}")->pluck('taname');
	}
	private function filter($param,$cidGids){
        $goodAttrModel = new GoodsAttr();
        $gidArr = [];
        foreach ($param as $gtid) {
            //如果点击的不是"不限"的时候
            //根据gtid得到商品的gid
            if($gtid){
                //查询到商品属性的名称
                $gtvalue = $goodAttrModel->where("gtid={$gtid}")->pluck('gtvalue');
                //通过名称查询到gid
                $gidArr[] = $goodAttrModel->where(['gtvalue'=>$gtvalue])->lists('gid');
            }
        }
        //p($gidArr);
        //如果$gidArr有值的时候,也就是用户不是全部"不限"的时候
        if($gidArr){
            $gid = $gidArr[0];
            foreach ($gidArr as $v) {
                $gid = array_intersect($v,$gid);
            }
            //因为$gid有可能是其他分类的gid,那么为了确保是当前分类,再次取交集
            $finalGids = array_intersect($gid,$cidGids);
        }else{//如果全部是"不限"的时候
            $finalGids = $cidGids;
        }
        return $finalGids;

        
    }
}
    
?>